tywx.AdManager = {
    retryCrossTimes: 3,
    retryBannerTimes: 3,
    AnimType: {
        STATIC: 1,
        SHAKE: 2,
        FRAME: 3
    },
    adNodeList: [],
    bannerNodeList: [],
    allAdInfoList: [],
    allBannerInfoList: [],
    rawAdInfoList: [],
    rawBannerInfoList: [],
    showAd: function (e, t) {
        var n = new tywx.AdManager.adNodeClass();
        n.adInfoList = JSON.parse(JSON.stringify(tywx.AdManager.allAdInfoList)), n.adType = 1,
            n.adTag = t || "", n.createAdNode(e), tywx.AdManager.adNodeList.push(n);
    },
    showBanner: function (e, t) {
        var n = new tywx.AdManager.adNodeClass();
        n.adInfoList = JSON.parse(JSON.stringify(tywx.AdManager.allBannerInfoList)), n.adType = 2,
            n.adTag = t || "", n.createAdNode(e), tywx.AdManager.bannerNodeList.push(n);
    },
    getAdNodeList: function () {
        return this.adNodeList;
    },
    getAdNodeByTag: function (e) {
        if (!e) return null;
        for (var t in this.adNodeList) if (this.adNodeList[t].adTag.toString() == e.toString()) return this.adNodeList[t];
        return null;
    },
    getBannerNodeList: function () {
        return this.bannerNodeList;
    },
    getBannerNodeByTag: function (e) {
        if (!e) return null;
        for (var t in this.bannerNodeList) if (this.bannerNodeList[t].adTag.toString() == e.toString()) return this.bannerNodeList[t];
        return null;
    },
    getAdInfoList: function () {
        return this.allAdInfoList;
    },
    getBannerInfoList: function () {
        return this.allBannerInfoList;
    },
    adNodeClass: function () {
        this.adType = 0, this.adIconBtn = null, this.currentAdInfo = null, this.currentWebPage = null,
            this.adInfoList = [], this.adTag = "";
    },
    adNodeObj: {
        createAdNode: function (e) {
            if (this.genRandomFirstAdInfo(), this.currentAdInfo) if (this.adIconBtn) this.adIconBtn.active = !0; else {
                var t = this;
                cc.loader.loadRes("prefabs/adNode", function (n, o) {
                    var i = cc.instantiate(o), a = e || {
                        x: 0,
                        y: 0
                    };
                    (i.position = cc.p(a.x, a.y), t.adIconBtn = i, cc.game.addPersistRootNode(i), t.adIconNode(),
                    t.adIconBtn && cc.isValid(t.adIconBtn)) ? t.adIconBtn.getChildByName("adButton").on("click", function () {
                        t.onClickAdIconBtn();
                    }) : console.error("cc.isValid(that.adIconBtn)");
                });
            }
        },
        genRandomFirstAdInfo: function () {
            var e = this;
            if (0 != this.adInfoList.length) {
                var t = [{
                    weight: 0,
                    id: "000"
                }];
                for (var n in this.adInfoList) {
                    var o = {
                        weight: parseInt(e.adInfoList[n].icon_weight),
                        id: e.adInfoList[n].icon_id
                    };
                    t.push(o);
                }
                t.sort(function (e, t) {
                    return e.weight > t.weight;
                });
                var i = 0;
                t.forEach(function (e) {
                    i += e.weight;
                });
                var a = parseInt(1e4 * Math.random()) % (i + 1), r = 0, s = 0;
                for (n = 0; n < t.length - 1; n++) if ((r += t[n].weight) < a && r + t[n + 1].weight >= a) {
                    s = n + 1;
                    break;
                }
                var l = t[s];
                this.adInfoList.forEach(function (t) {
                    t.icon_id == l.id && (e.currentAdInfo = t);
                });
            }
        },
        genRandomSecondAdInfo: function () {
            var e = this, t = this.currentAdInfo.webpages;
            if (void 0 !== t && 0 != t.length) {
                var n = [{
                    weight: 0,
                    id: "000"
                }];
                for (var o in t) {
                    var i = {
                        weight: parseInt(t[o].webpage_weight),
                        id: t[o].config_id
                    };
                    n.push(i);
                }
                n.sort(function (e, t) {
                    return e.weight > t.weight;
                });
                var a = 0;
                n.forEach(function (e) {
                    a += e.weight;
                });
                var r = parseInt(1e4 * Math.random()) % (a + 1), s = 0, l = 0;
                for (o = 0; o < n.length - 1; o++) if ((s += n[o].weight) < r && s + n[o + 1].weight >= r) {
                    l = o + 1;
                    break;
                }
                var c = n[l];
                t.forEach(function (t) {
                    t.config_id == c.id && (e.currentWebPage = t);
                });
            }
        },
        adIconNode: function () {
            if (this.currentAdInfo && this.adIconBtn) {
                var e = this.currentAdInfo.icon_type, t = this;
                if (this.adIconBtn.getChildByName) {
                    var n = this.adIconBtn.getChildByName("adIcon"),
                        o = this.adIconBtn.getChildByName("adButton");
                    switch (n.stopAllActions(), n.removeComponent(cc.Animation), n.setRotation(0), e) {
                        case tywx.AdManager.AnimType.STATIC:
                            cc.loader.load({
                                url: t.currentAdInfo.icon_url[0]
                            }, function (e, t) {
                                e || (n.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(t), t && t.width && t.height && (n.setContentSize(cc.size(t.width, t.height)),
                                    o.setContentSize(cc.size(t.width, t.height))));
                            });
                            break;

                        case tywx.AdManager.AnimType.SHAKE:
                            cc.loader.load({
                                url: t.currentAdInfo.icon_url[0]
                            }, function (e, t) {
                                n.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(t), t && t.width && t.height && (n.setContentSize(cc.size(t.width, t.height)),
                                    o.setContentSize(cc.size(t.width, t.height))), n.anchorX = .5, n.anchorY = .5;
                                var i = cc.rotateBy(.06, -20), a = cc.rotateBy(.12, 40),
                                    r = cc.rotateBy(.12, -40), s = cc.rotateBy(.06, 20), l = cc.delayTime(1);
                                n.runAction(cc.repeatForever(cc.sequence(i, cc.repeat(cc.sequence(a, r), 4), s, l)));
                            });
                            break;

                        case tywx.AdManager.AnimType.FRAME:
                            var i = [];
                            cc.loader.load(t.currentAdInfo.icon_url, function (e, a) {
                                if (e) for (var r = 0; r < e.length; r++) cc.log("Error url [" + e[r] + "]: " + a.getError(e[r]));
                                for (r = 0; r < t.currentAdInfo.icon_url.length; r++) if (a.getContent(t.currentAdInfo.icon_url[r])) {
                                    var s = new cc.SpriteFrame(a.getContent(t.currentAdInfo.icon_url[r]));
                                    i.push(s);
                                }
                                (function () {
                                    n.stopAllActions(), n.removeComponent(cc.Animation);
                                    var e = i[0].getTexture();
                                    e && e.width && e.height && (n.setContentSize(cc.size(e.width, e.height)), o.setContentSize(cc.size(e.width, e.height)));
                                    var a = t.currentAdInfo.time_interval, r = a && a > 0 ? 1e3 / a : 10,
                                        s = n.addComponent(cc.Animation),
                                        l = cc.AnimationClip.createWithSpriteFrames(i, r);
                                    l.name = "anim_frame", l.wrapMode = cc.WrapMode.Loop, s.addClip(l), s.play("anim_frame");
                                })();
                            });
                    }
                }
            }
        },
        onClickAdIconBtn: function () {
            try {
                this.genRandomSecondAdInfo();
                var e = this.currentAdInfo.icon_skip_type, t = this.currentAdInfo.toappid,
                    n = this.currentAdInfo.togame, o = this.currentAdInfo.path,
                    i = this.currentAdInfo.second_toappid;
                console.log("topath ====>" + o);
                var a = this, r = this.currentAdInfo.icon_id, s = "0", l = "", c = "0";
                this.currentWebPage && 1 == this.adType && (l = this.currentWebPage.webpage_url,
                    s = this.currentWebPage.config_id, c = this.currentWebPage.webpage_id);
                var d = [r, s, l, t, n, c, a.adType];
                if (console.log("bi_paramlist ====> " + JSON.stringify(d)), tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickAdBtn, d),
                wx && wx.navigateToMiniProgram) return 1 == e ? void wx.navigateToMiniProgram({
                    appId: t,
                    path: o || "?from=adcross",
                    envVersion: "release",
                    extraData: {
                        from: o || "?from=adcross"
                    },
                    success: function (e) {
                        tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickDirectToMiniGameSuccess, d),
                            console.log("wx.navigateToMiniProgram success"), console.log(e);
                    },
                    fail: function (e) {
                        tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickDirectToMiniGameFail, d),
                            console.log("wx.navigateToMiniProgram fail"), console.log(e);
                    },
                    complete: function (e) {
                        console.log("navigateToMiniProgram ==== complete"), a.resetBtnIcon();
                    }
                }) : void (2 == e ? wx.navigateToMiniProgram({
                    appId: i,
                    path: o || "?from=adcross",
                    envVersion: "release",
                    extraData: {
                        from: o || "?from=adcross"
                    },
                    success: function (e) {
                        tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickDirectToMiniGameSuccess, d),
                            console.log("wx.navigateToMiniProgram success"), console.log(e);
                    },
                    fail: function (e) {
                        tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickDirectToMiniGameFail, d),
                            console.log("wx.navigateToMiniProgram fail"), console.log(e);
                    },
                    complete: function (e) {
                        a.resetBtnIcon(), console.log("navigateToMiniProgram ==== complete");
                    }
                }) : console.error("Unsupported skip type! Please Check!"));
                if (!a.currentWebPage || !a.currentWebPage.webpage_url) return void a.resetBtnIcon();
                tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickShowQRCode, d),
                tywx.IsWechatPlatform() && wx.previewImage({
                    current: [a.currentWebPage.webpage_url],
                    urls: [a.currentWebPage.webpage_url],
                    success: function (e) {
                        tywx.LOGD(null, "预览图片成功！");
                    },
                    fail: function (e) {
                        tywx.LOGD(null, "预览图片失败！");
                    },
                    complete: function (e) {
                        console.log("预览图片完成"), a.resetBtnIcon();
                    }
                });
            } catch (e) {
                console.log("error:", "tywx.AdManager.onClickAdIconBtn——" + JSON.stringify(e));
            }
        },
        resetBtnIcon: function () {
            this.adIconBtn && (this.genRandomFirstAdInfo(), this.adIconNode());
        },
        onForeGround: function () {
            this.adIconNode();
        },
        showAdNode: function () {
            this.adIconBtn && this.adIconBtn && (this.adIconBtn.active = !0);
        },
        hideAdNode: function () {
            this.adIconBtn && this.adIconBtn && (this.adIconBtn.active = !1);
        }
    },
    freshAdIconByTime: function () {
        tywx.AdManager.adNodeList.forEach(function (e) {
            e && e.resetBtnIcon && e.resetBtnIcon();
        });
    },
    freshAdBannerByTime: function () {
        tywx.AdManager.bannerNodeList.forEach(function (e) {
            e && e.resetBtnIcon && e.resetBtnIcon();
        });
    },
    startFreshAdIcon: function () {
        try {
            var e = 10;
            if (this.allAdInfoList && this.allAdInfoList.length && this.allAdInfoList.length > 0) {
                for (var t = 0; t < this.allAdInfoList.length; t++) {
                    if (o = this.allAdInfoList[t].icon_interval) {
                        e = parseInt(o) > 0 ? parseInt(o) : e;
                        break;
                    }
                }
                clearInterval(tywx.AdManager.freshAdIconByTime), setInterval(tywx.AdManager.freshAdIconByTime, 1e3 * e);
            }
            var n = 10;
            if (this.allBannerInfoList && this.allBannerInfoList.length && this.allBannerInfoList.length > 0) {
                for (t = 0; t < this.allBannerInfoList.length; t++) {
                    var o;
                    if (o = this.allBannerInfoList[t].icon_interval) {
                        n = parseInt(o) > 0 ? parseInt(o) : n;
                        break;
                    }
                }
                clearInterval(tywx.AdManager.freshAdBannerByTime), setInterval(tywx.AdManager.freshAdBannerByTime, 1e3 * n);
            }
        } catch (e) {
            console.error("Error: startFreshAdIcon==>" + JSON.stringify(e));
        }
    },
    onForeGround: function () {
        this.freshAdIconByTime(), this.freshAdBannerByTime(), this.startFreshAdIcon();
    },
    getConfigSignStr: function (e) {
        for (var t = Object.keys(e).sort(), n = "", o = 0; o < t.length; o++) {
            var i = t[o];
            "act" != i && "sign" != i && (n += i + "=" + e[i]);
        }
        return tywx.hex_md5("market.tuyoo.com-api-" + n + "-market.tuyoo-api") || "";
    },
    requestADInfo: function () {
        try {
            if (!tywx.IsWechatPlatform()) return;
            this.retryCrossTimes--;
            var e = {}, t = new Date().getTime();
            e.act = "api.getCrossConfig", e.time = t, e.game_mark = tywx.SystemInfo.cloudId + "-" + tywx.SystemInfo.gameId;
            var n = this.getConfigSignStr(e), o = [];
            for (var i in e) o.push(i + "=" + e[i]);
            o.push("sign=" + n);
            var a = tywx.SystemInfo.shareManagerUrl + "?" + o.join("&"), r = this;
            wx.request({
                url: a,
                method: "GET",
                success: function (e) {
                    if (200 == e.statusCode) {
                        var t = e.data;
                        r.allAdInfoList = [], t.retmsg && (r.rawAdInfoList = t.retmsg, r.processRawConfigInfo(),
                            tywx.NotificationCenter.trigger(tywx.EventType.GET_ADMANAGER_ICON_INFO_SUCCESS)),
                            r.retryCrossTimes = 3;
                    } else r.retryCrossTimes > 0 ? r.requestADInfo() : r.retryCrossTimes = 3;
                },
                fail: function (e) {
                    r.retryCrossTimes > 0 ? r.requestADInfo() : r.retryCrossTimes = 3;
                }
            });
        } catch (e) {
            tywx.LOGE("error:", "tywx.AdManager.requestADInfo——" + JSON.stringify(e));
        }
    },
    requestBannerInfo: function () {
        try {
            if (!tywx.IsWechatPlatform()) return;
            this.retryBannerTimes--;
            var e = {}, t = new Date().getTime();
            e.act = "api.getBannerConfig", e.time = t, e.game_mark = tywx.SystemInfo.cloudId + "-" + tywx.SystemInfo.gameId;
            var n = this.getConfigSignStr(e), o = [];
            for (var i in e) o.push(i + "=" + e[i]);
            o.push("sign=" + n);
            var a = tywx.SystemInfo.shareManagerUrl + "?" + o.join("&"), r = this;
            wx.request({
                url: a,
                method: "GET",
                success: function (e) {
                    if (200 == e.statusCode) {
                        var t = e.data;
                        r.allBannerInfoList = [], t.retmsg && (r.rawBannerInfoList = t.retmsg, r.processRawConfigInfo(),
                            tywx.NotificationCenter.trigger(tywx.EventType.GET_ADMANAGER_BANNER_INFO_SUCCESS)),
                            r.retryBannerTimes = 3;
                    } else r.retryBannerTimes > 0 ? r.requestBannerInfo() : r.retryBannerTimes = 3;
                },
                fail: function (e) {
                    r.retryBannerTimes > 0 ? r.requestBannerInfo() : r.retryBannerTimes = 3;
                }
            });
        } catch (e) {
            tywx.LOGE("error:", "tywx.AdManager.requestADInfo——" + JSON.stringify(e));
        }
    },
    requestLocalIPInfo: function () {
        var e = this;
        wx.request({
            url: "https://iploc.ywdier.com/api/iploc5/search/city",
            success: function (t) {
                200 == t.statusCode && (t.data && t.data.loc && (e.ipLocInfo = t.data), e.processRawConfigInfo()),
                    console.error("requestLocalIPInfo ==>" + JSON.stringify(t));
            },
            fail: function (e) {
            }
        });
    },
    processRawConfigInfo: function () {
        var e = this;
        if (this.allBannerInfoList = [], this.allAdInfoList = [], this.ipLocInfo && this.ipLocInfo.loc && this.ipLocInfo.loc[1]) {
            var t = this.ipLocInfo.loc[1];
            this.rawAdInfoList.forEach(function (n) {
                (void 0 == n.icon_weight || Math.floor(n.icon_weight) <= .1) && (n.icon_weight = 0),
                    n.icon_weight = 10;
                var o = !0;
                if (n.province && n.province instanceof Array) if (0 == n.province.length) o = !1; else for (var i in n.province) {
                    if (n.province[i].indexOf(t) > -1) {
                        o = !1;
                        break;
                    }
                } else o = !1;
                o || e.allAdInfoList.push(n);
            }), this.rawBannerInfoList.forEach(function (n) {
                (void 0 == n.icon_weight || Math.floor(n.icon_weight) <= .1) && (n.icon_weight = 0),
                    n.icon_weight = 10;
                var o = !0;
                if (n.province && n.province instanceof Array) if (0 == n.province.length) o = !1; else for (var i in n.province) {
                    if (n.province[i].indexOf(t) > -1) {
                        o = !1;
                        break;
                    }
                } else o = !1;
                o || e.allBannerInfoList.push(n);
            });
        } else this.rawAdInfoList.forEach(function (t) {
            (void 0 == t.icon_weight || Math.floor(t.icon_weight) <= .1) && (t.icon_weight = 0),
            (!t.province || t.province instanceof Array && 0 == t.province.length) && e.allAdInfoList.push(t);
        }), this.rawBannerInfoList.forEach(function (t) {
            (void 0 == t.icon_weight || Math.floor(t.icon_weight) <= .1) && (t.icon_weight = 0),
            (!t.province || t.province instanceof Array && 0 == t.province.length) && e.allBannerInfoList.push(t);
        });
    },
    init: function () {
        this.requestLocalIPInfo(), this.requestADInfo(), this.requestBannerInfo();
    }
}, tywx.AdManager.adNodeClass.prototype = tywx.AdManager.adNodeObj
